package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.basic.vali.Add;
import cn.itsource.basic.vali.Update;
import cn.itsource.org.domain.Department;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 接口类：部门
 */
@RestController
@RequestMapping("/department")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 接口：查询单个对象
     * @param id
     * @return
     */
    @ApiOperation(value = "查询单个对象")
    @GetMapping("/{id}")
    public Department findOne(@PathVariable Long id){
        return departmentService.findOne(id);
    }

    /**
     * 接口：查询所有对象
     * @return
     */
    @ApiOperation(value = "询所有对象")
    @GetMapping
    public List<Department> findAll(){
        return departmentService.findAll();
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable Long id){
        departmentService.del(id);
        return new AjaxResult();
    }


    /**
     * 接口：批量删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除")
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids){
        departmentService.patchDel(ids);
        return new AjaxResult();
    }

    /**
     * 接口：添加或修改
     * @param department
     * @return
     */
    @ApiOperation(value = "添加或修改")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody @Validated Department department){
        if(department.getId()==null){ //添加
            departmentService.add(department);
        }else{
            departmentService.update(department);
        }
        return new AjaxResult();
    }

    /**
     * 接口：分页+高级查询
     * @param departmentQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页+高级查询")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryPage(departmentQuery);
    }


    /**
     * 查询部门树
     * @return
     */
    @ApiOperation(value = "查询部门树")
    @GetMapping("/deptTree")
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }

    @PutMapping("/add")
    public AjaxResult add(@RequestBody @Validated({Add.class}) Department department){
        return new AjaxResult();
    }
    @PutMapping("/update")
    public AjaxResult update(@RequestBody @Validated({Update.class}) Department department){
        return new AjaxResult();
    }
    @PutMapping("/order")
    public AjaxResult order(@RequestBody @Validated Department department){
        return new AjaxResult();
    }

}
